[pull] main from MetaMask:main#585
Merged
Merged
Conversation
## **Description**
Replaces per-section \`PerpsConnectionProvider\` connect/disconnect
calls with a single top-level \`PerpsAlwaysOnProvider\` mounted at the
wallet root.
**Problem**: Multiple \`PerpsConnectionProvider\` instances (Homepage,
PerpsTabView, ActivityView, TrendingView, ExploreSearchScreen,
UrlAutocomplete) each called \`connect()\`/\`disconnect()\`, creating
reference-count races in \`PerpsConnectionManager\`. This caused
intermittent bugs — positions not showing, 24h values missing — after
long app backgrounding sessions.
**Solution**: \`PerpsAlwaysOnProvider\` is the sole caller of
\`PerpsConnectionManager.connect()\` and \`disconnect()\`.
\`PerpsConnectionProvider\` is now a pure React context source — it
polls singleton state and exposes it via \`PerpsConnectionContext\`, but
never calls connect/disconnect itself.
Key changes:
- New \`PerpsAlwaysOnProvider\` — mounts once **inside**
\`ErrorBoundary\` at \`Wallet/index.tsx\`, owns \`AppState\` listener,
sole connect/disconnect owner
- \`PerpsConnectionProvider\` stripped of all lifecycle management:
removed \`manageLifecycle\` prop, \`isVisible\` prop, and the
\`usePerpsConnectionLifecycle\` hook call
- Deleted \`usePerpsConnectionLifecycle\` hook (its responsibilities
moved to \`PerpsAlwaysOnProvider\`)
- All section-level \`PerpsConnectionProvider\` instances kept as
context sources (required by \`useContext(PerpsConnectionContext)\`
consumers in \`sections.config.tsx\` et al.)
- Restored \`PerpsConnectionProvider\` (context-only) in
\`ExploreSearchScreen\`, \`UrlAutocomplete\`, and \`TrendingView\`
\`SectionWrapper\` — these render \`useSectionData\` which reads
\`PerpsConnectionContext\`
- Removed visibility callback plumbing (\`onVisibilityChange\`,
\`perpsVisibilityCallback\`) from \`WalletTokensTabView\` and
\`PerpsTabViewWithProvider\`
- Updated architecture docs
## **Changelog**
CHANGELOG entry: null
## **Related issues**
Fixes:
## **Manual testing steps**
\`\`\`gherkin
Feature: Perps always-on connection
Scenario: user backgrounds and foregrounds the app
Given perps feature flag is enabled
And user is on the Wallet screen
When user opens the Perps tab
Then positions and market data load correctly
When user backgrounds the app for 30+ seconds
And user foregrounds the app
Then Perps data reconnects and loads correctly
Scenario: user switches between tabs
Given perps feature flag is enabled
When user navigates between Wallet / Activity / Trending tabs
Then perps data remains connected without errors
And connectionRefCount stays at 1
Scenario: user switches accounts
Given perps feature flag is enabled
When user switches to a different account
Then Perps data refreshes for the new account
\`\`\`
## **Screenshots/Recordings**
### **Before**
Multiple providers each managing lifecycle → reference-count races on
background/foreground.
### **After**
Single \`PerpsAlwaysOnProvider\` owns the lifecycle —
\`connectionRefCount\` stays exactly 1. \`PerpsConnectionProvider\` is a
context-only provider with no lifecycle responsibilities.
## **Pre-merge author checklist**
- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I've included tests if applicable
- [ ] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **Pre-merge reviewer checklist**
- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes Perps WebSocket connection ownership from many screen-scoped
providers to a single wallet-root lifecycle manager, which can impact
reconnect behavior, resource usage, and perps data availability across
the app.
>
> **Overview**
> **Centralizes Perps WebSocket lifecycle management** by introducing
`PerpsAlwaysOnProvider` (mounted at the `Wallet` root) as the *only*
component that calls `PerpsConnectionManager.connect()`/`disconnect()`,
driven by `AppState` background/foreground transitions with a delayed
reconnect and error logging.
>
> `PerpsConnectionProvider` is simplified to a context/state wrapper
only: it no longer takes `isVisible`, no longer unmounts children when
hidden, and removes the `usePerpsConnectionLifecycle` hook entirely
(hook + tests deleted, barrel export removed). Call sites (Perps tab,
Activity perps transactions tab, homepage Perps section, Explore search,
UrlAutocomplete, Trending sections) are updated to stop passing
visibility props and to use `suppressErrorView` where needed.
>
> Tests and docs are updated to match the new always-on architecture,
including new coverage for `PerpsAlwaysOnProvider` and adjusted
`PerpsConnectionProvider` retry/error behavior.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
1644485. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )